package com.changgou.service.goods.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.changgou.common.pojo.PageResult;
import com.changgou.goods.pojo.Brand;
import com.changgou.service.goods.dao.BrandMapper;
import com.changgou.service.goods.service.BrandService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {
    @Resource
    private BrandMapper brandMapper;

    /**
     * 品牌列表查询
     *
     * @return
     */
    @Override
    public List<Brand> findList() {
        List<Brand> brandList = brandMapper.selectList(new LambdaQueryWrapper<>());
        return brandList;
    }

    /**
     * 品牌修改
     *
     * @param brand
     */
    @Override
    public void updateByIds(Integer id, Brand brand) {
        brand.setId(id);
        brandMapper.updateById(brand);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageResult findPage(Integer page, Integer size) {
        //1. 构建分页对象
        Page<Brand> p = new Page<>(page, size);
        //2. 查询数据
        this.brandMapper.selectPage(p, null);
        return new PageResult(p.getTotal(), p.getRecords());
    }

    /***
     * 多条件搜索品牌数据
     * @param searchMap
     * @return
     */
    @Override
    public List<Brand> searchMap(Map<String, Object> searchMap) {
        LambdaQueryWrapper<Brand> wrapper = Wrappers.<Brand>lambdaQuery()
                .like(ObjectUtil.isNotEmpty(searchMap.get("name")), Brand::getName, searchMap.get("name"))
                .eq(ObjectUtil.isNotEmpty(searchMap.get("letter")), Brand::getLetter, searchMap.get("letter"));
        return this.brandMapper.selectList(wrapper);
    }
//    /**
//     * 品牌列表条件查询
//     * @param searchMap
//     * @return
//     */
//    @Override
//    public List<Brand> list(Map<String, Object> searchMap) {
//        Example example = new Example(Brand.class);
//        //封装查询条件
//        Example.Criteria criteria = example.createCriteria();
//        if (searchMap!=null){
//            //品牌名称(模糊) like  %
//            if (searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
//                criteria.andLike("name","%"+searchMap.get("name")+"%");
//            }
//            //按照品牌首字母进行查询(精确)
//            if (searchMap.get("letter")!=null && !"".equals(searchMap.get("letter"))){
//                criteria.andEqualTo("letter",searchMap.get("letter"));
//            }
//        }
//        List<Brand> brandList = brandMapper.selectByExample(example);
//        return brandList;
//    }
//
//    @Override
//    public Page<Brand> findPage(int page, int size) {
//        PageHelper.startPage(page,size);
//        Page<Brand> page1 = (Page<Brand>) brandMapper.selectAll();
//        return page1;
//    }
//
//    @Override
//    public Page<Brand> findPage(Map<String, Object> searchMap, int page, int size) {
//        //设置分页
//        PageHelper.startPage(page,size);
//
//        //设置查询条件
//        Example example = new Example(Brand.class);
//        Example.Criteria criteria = example.createCriteria();
//        if (searchMap!=null){
//            //设置品牌名称模糊查询
//            if (searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
//                criteria.andLike("name","%"+searchMap.get("name")+"%");
//            }
//            //设置品牌首字母的精确查询
//            if (searchMap.get("letter")!=null && !"".equals(searchMap.get("letter"))){
//                criteria.andEqualTo("letter",searchMap.get("letter"));
//            }
//        }
//        Page<Brand> pageInfo = (Page<Brand>) brandMapper.selectByExample(example);
//        return pageInfo;
//    }
}
